package com.zhitong.money.controller;


import com.github.pagehelper.PageInfo;
import com.zhitong.money.entity.AjaxResponse;
import com.zhitong.money.entity.Dept;
import com.zhitong.money.exception.RepeatException;
import com.zhitong.money.services.api.IDeptService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@Slf4j
@RestController
public class DeptContro {

    @Autowired
    private IDeptService deptService;

    /**
     *@Author: AndyZhang
     *@Description 获取所有部门
     *@param
     *@return
     *@Date: 17:19 2021/1/28
     */
    @GetMapping("/depts")
    public AjaxResponse getAllDept() throws Exception{
            List<Dept>  allDept = deptService.findAllDept();
            return AjaxResponse.success(allDept);
    }

    /**
     *@Author: AndyZhang
     *@Description 根据条件查询部门
     *@param
     *@return
     *@Date: 17:20 2021/1/28
     */
    @PostMapping("/deptMap")
    public AjaxResponse getDeptsByMap(@RequestBody Map<String,Object> map) throws Exception{
            List<Dept> deptByMap = deptService.findDeptByMap(map);
            return AjaxResponse.success(deptByMap);
    }

    /**
     *@Author: AndyZhang
     *@Description 根据id获取部门
     *@param
     *@return
     *@Date: 17:20 2021/1/28
     */
    @GetMapping("/dept/{id}")
    public AjaxResponse getDeptById(@PathVariable("id")String id) throws Exception{
        Dept deptById = deptService.findDeptById(id);
       return AjaxResponse.success(deptById);
    }


    /**
     *@Author: AndyZhang
     *@Description 分页查询部门
     *@param
     *@return
     *@Date: 17:20 2021/1/28
     */
    @PostMapping("/depts")
    public AjaxResponse getDeptByPage(@RequestBody Map<String,Object> map) throws Exception{
        PageInfo deptByPage = deptService.findDeptByPage(map);
        return AjaxResponse.success(deptByPage);
    }

    /**
     *@Author: AndyZhang
     *@Description 添加部门
     *@param
     *@return
     *@Date: 17:20 2021/1/28
     */
    @RequiresPermissions({"addDept"})
    @PostMapping("/dept")
    public  AjaxResponse addDept(@RequestBody Map<String,Object> map) throws Exception{
            log.info(String.valueOf(map));
            deptService.addDept(map);
        return AjaxResponse.success("添加成功");
    }

    @RequiresPermissions({"updateDept"})
    @PutMapping("/dept")
    public AjaxResponse updateDept(@RequestBody Map<String,Object> map) throws Exception{
            deptService.updateDept(map);
        AjaxResponse success = AjaxResponse.success();
        success.setMessage("更新成功!");
        return success;
    }

    @RequiresPermissions({"delDept"})
    @DeleteMapping("/dept/{id}")
    public AjaxResponse deleteDept(@PathVariable("id") String id) throws Exception{
        int i = deptService.delDeptById(id);
        if (i>0){
            AjaxResponse success = AjaxResponse.success();
            success.setMessage("删除成功!");
            return success;
        }
        return AjaxResponse.fail("该部门有职位未删除!");
    }

    @RequiresPermissions({"delDept"})
    @PostMapping("/deldepts")
    public AjaxResponse deleteDepts(@RequestBody List<String> ids) throws Exception{
        log.info(ids.toString());
        if (ids==null||ids.isEmpty()){
            return AjaxResponse.fail("请选择部门!");
        }
        int i = deptService.delDeptByIds(ids);
        if (i>0){
            AjaxResponse success = AjaxResponse.success();
            success.setMessage("删除成功!");
            return success;
        }
        return AjaxResponse.fail("部门有职位未删除!");
    }


}
